<?php

namespace app\admin\controller;

use nsCms\Form;

class Goods extends Common
{
    public function index()
    {
        if (request()->isPost()) {
            $key = input('post.key');
            $list = db('goods')
                ->where('title|description|content', 'like', "%" . $key . "%")
                ->order('create_time desc')
                ->paginate(getPage())
                ->toArray();
            foreach ($list['data'] as $k => $v) {
                $list['data'][$k]['create_time'] = date('Y-m-d H:s', $v['create_time']);
            }
            return ['code' => 0, 'msg' => '获取成功!', 'data' => $list['data'], 'count' => $list['total']];
        }
        return $this->fetch();
    }
    
    //修改
    public function edit()
    {
        if (request()->isPost()) {
            $data = input('post.');
            unset($data['file']);
            $md = new \app\common\model\Goods();
            
            if (isset_full($data, 'imgs')) {
                $data['imgs'] = implode(';', $data['imgs']);
            }
            
            if (isset_full($data, 'id')) {
                $data['update_time'] = time();
                $data['userid'] = session('admin_id');
                $md->allowField(true)->update($data);
            } else {
                unset($data['id']);
                $data['create_time'] = time();
                $md->allowField(true)->insert($data);
            }
            $result['code'] = 1;
            $result['msg'] = '操作成功!';
            $result['url'] = url('goods/index');
            return $result;
        } else {
            $id = input('param.id');
            $info = db('goods')->where(['id' => $id])->find();
            $form = new Form();
            $cat_str = $form->godos_cat('cid', '分类', isset_full($info, 'cid') ? $info['cid'] : 0);
            $img_str = $form->image(['field' => 'img', 'name' => '封面', 'setup' => [], 'class' => 'img'], isset_full($info, 'img') ? $info['img'] : '');
            $imgs_str = $form->images(['field' => 'imgs', 'name' => '产品图', 'setup' => [], 'class' => 'imgs'], isset_full($info, 'imgs') ? $info['imgs'] : '');
            $c_str = $form->editor(['field' => 'img', 'name' => '封面', 'setup' => [
                'toolbar'         => 'full',
                'default'         => '',
                'height'          => '',
                'showpage'        => '1',
                'enablekeylink'   => '0',
                'replacenum'      => '',
                'enablesaveimage' => '0',
                'flashupload'     => '1',
                'alowuploadexts'  => '',
            ], 'class'                      => 'content'], isset_full($info, 'content') ? $info['content'] : '');
            $this->assign('cat_str', $cat_str);
            $this->assign('img_str', $img_str);
            $this->assign('imgs_str', $imgs_str);
            $this->assign('c_str', $c_str);
            $this->assign('info', $info);
            $this->assign('title', empty($id) ? '添加分类' : '编辑分类');
            return $this->fetch();
        }
    }
    
    //商品分类
    public function cat_list()
    {
        if (request()->isPost()) {
            $key = input('post.key');
            $list = db('goods_cat')
                ->where('title', 'like', "%" . $key . "%")
                ->order('create_time desc')
                ->paginate(getPage())
                ->toArray();
            foreach ($list['data'] as $k => $v) {
                $list['data'][$k]['create_time'] = date('Y-m-d H:s', $v['create_time']);
                $list['data'][$k]['p_name'] = $v['pid'] == 0 ? '顶级分类' : db('goods_cat')->where('id', $v['pid'])->value('title');
            }
            return ['code' => 0, 'msg' => '获取成功!', 'data' => $list['data'], 'count' => $list['total']];
        }
        return $this->fetch();
    }
    
    //状态
    public function cat_state()
    {
        $id = input('post.id');
        $status = input('post.status');
        if (db('goods_cat')->where('id=' . $id)->update(['status' => $status]) !== false) {
            return ['code' => 1, 'msg' => '设置成功!'];
        } else {
            return ['code' => 0, 'msg' => '设置失败!'];
        }
    }
    
    //排序
    public function cat_order()
    {
        $data = input('post.');
        if (db('goods_cat')->update($data) !== false) {
            return ['msg' => '操作成功！', 'url' => url('index'), 'code' => 1];
        } else {
            return ['code' => 0, 'msg' => '操作失败！'];
        }
    }
    
    //修改
    public function cat_edit()
    {
        if (request()->isPost()) {
            $data = input('post.');
            if (isset_full($data, 'id')) {
                $data['update_time'] = time();
                $data['userid'] = session('admin_id');
                db('goods_cat')->update($data);
            } else {
                unset($data['id']);
                $data['create_time'] = time();
                db('goods_cat')->insert($data);
            }
            $result['code'] = 1;
            $result['msg'] = '操作成功!';
            $result['url'] = url('cat_list');
            cache('linkList', null);
            return $result;
        } else {
            $id = input('param.id');
            $info = db('goods_cat')->where(['id' => $id])->find();
            $p_list = db('goods_cat')->where(['pid' => 0])->select();
            $this->assign('p_list', $p_list);
            $this->assign('info', $info);
            $this->assign('title', empty($id) ? '添加分类' : '编辑分类');
            return $this->fetch();
        }
    }
    
    //删除
    public function cat_del()
    {
        db('goods_cat')->where(['id' => input('id')])->delete();
        return ['code' => 1, 'msg' => '删除成功！'];
    }
}